package cn.eonshine.cuba.lsms.modular.biz.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.util.DateUtils;
import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.biz.entity.RoadGps;
import cn.eonshine.cuba.lsms.modular.CommonService;
import cn.eonshine.cuba.lsms.modular.biz.entity.EVehicleExecutionInformation;
import cn.eonshine.cuba.lsms.modular.biz.mapper.EVehicleExecutionInformationMapper;
import cn.eonshine.cuba.lsms.modular.biz.service.IRoadGPSService;

@Service
public class RoadGPSService extends CommonService<RoadGps> implements IRoadGPSService {

	
	@Autowired
	private EVehicleExecutionInformationMapper vehicleExecutionInformationMapper;
	
	private final String listUrl = "/roadGps/list";
	private final String updateUrl = "/roadGps/update";
	
	@Override
	public void requestRoadGPS() {
		requestData(listUrl, updateUrl, InfStatus.INF_LOG_TYPE.AUTOMOTOR_GPS_NUM);
	}

	@Transactional
	@Override
	protected Object dataHandle(RoadGps obj) throws Exception {
		if(StringUtils.isBlank(obj.getRoadTankerId())) {
			throw new Exception("There is no chapa : "+obj);
		}
		try {
			QueryWrapper<EVehicleExecutionInformation> query = new QueryWrapper<EVehicleExecutionInformation>();
			query.lambda().eq(EVehicleExecutionInformation::getVehicleNo, obj.getRoadTankerId());
			query.lambda().eq(EVehicleExecutionInformation::getIsHis, InfStatus.GPS_IS_HIS_NO);
			EVehicleExecutionInformation currentVehicle = vehicleExecutionInformationMapper.selectOne(query);
			// set current data to history  
			if(currentVehicle != null) {
				currentVehicle.setIsHis(InfStatus.GPS_IS_HIS_YES);
				vehicleExecutionInformationMapper.updateById(currentVehicle);
			}

			// insert gps
			EVehicleExecutionInformation info = new EVehicleExecutionInformation();
			info.setVehicleNo(obj.getRoadTankerId());
			info.setLongitude(BigDecimal.valueOf(obj.getLongitude()));
			info.setLatitude(BigDecimal.valueOf(obj.getLatitude()));
			info.setDataTime(obj.getCreateDate());
			info.setIsHis(InfStatus.GPS_IS_HIS_NO);
			info.setCreateBy(InfStatus.CREATE_BY);
			info.setUpdateBy(InfStatus.UPDATE_BY);
			if(currentVehicle != null) {
				info.setCreateDate(currentVehicle.getCreateDate());
			}else {
				info.setCreateDate(DateUtils.getCubaDate(Calendar.getInstance().getTime()));
			}
			info.setUpdateDate(obj.getDataInputDatetime());
			vehicleExecutionInformationMapper.insert(info);
			
			return info;
		} catch (Exception e) {
			throw new Exception("There is multi Vehicle in SISTLOG : "+obj);
		}
	}

	@Override
	protected List<RoadGps> feadBackDataHandle(Object obj, RoadGps t, List<RoadGps> feadBackDataLists)
			throws Exception {
//		RoadGps info = (RoadGps) obj;
		
		t.setLsmsUpdate(InfStatus.LSMS_UPDATE_EXTRACT_YES);
		feadBackDataLists.add(t);
		return feadBackDataLists;
	}

}
